package com.erp.module.yuliaofreight.YuliaoFreight

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.YuliaoCangweiBean
import com.erp.module.yuliaofreight.YuliaoFreightConteract
import com.erp.module.yuliaofreight.YuliaoFreightModel
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class IYuliaoCangweiP(var V: YuliaoFreightConteract.YuliaoFreightView,var context :Context,var cangweilist: MutableList<YuliaoCangweiBean>): YuliaoFreightConteract.YuliaoFreight {

    var md = YuliaoFreightModel()

    override fun cjYuliaoCangwei(name: String) {
        md.createCangweiData(context,name.trim(),object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.cjYuliaoCangweiOk(data.toString())
            }

            override fun onNot(throwable: String?) {
                V.cjYuliaoCangweiNo(throwable.toString())
            }

        })
    }

    override fun xgYuliaoCangwei(id: String,name: String) {
        md.updateCangweiData(context,id,name.trim(),object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.xgYuliaoCangweiOk(data.toString())
            }

            override fun onNot(throwable: String?) {
                V.xgYuliaoCangweiNo(throwable.toString())
            }

        })
    }

    override fun getCangweiData() {
        md.getCangweiData(context,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                JsonCangwei(data)
            }

            override fun onNot(throwable: String?) {
                V.getCangweiDataNo()
            }

        })
    }

    fun JsonCangwei(data: String?){
        if (data==null){
            return
        }
        cangweilist.clear()
        try {
            val j = JSONObject(data)
            val js = JsonUtil.getjsvolue(j,"data")
            for (i in 0 until js.length()) {
                var bean = YuliaoCangweiBean()
                val jo = js.get(i) as JSONObject
                bean.yfs_id = JsonUtil.getvolue(jo, "ysf_id")
                bean.name = JsonUtil.getvolue(jo, "freight_space_name")
                cangweilist.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        V.getCangweiDataOk()
    }

}